//Program by:
//Ben Schmidt - bjs778 - 11054018
//Mike Nimeck - mfn028 - 11053045
//Jeff Huang - jlh105 - 11050226

// Fireworks update vertex shader
#version 330

// Position and velocity inputs
layout (location = 0) in vec3 particle_position;
layout (location = 1) in vec3 particle_velocity;

// Outputs (via transform feedback)
out vec3 position_out;
out vec3 velocity_out;

// Parameters...
// This has to match the app's view of the world - no default is given here.
uniform int num_particles;

// These all have defaults. In the example application, these aren't changed.
// Just edit these and rerun the application. It's certainly possible to change
// these parameters at run time by hooking the uniforms up in the application.
uniform float rule1_weight = 0.17;
uniform float rule2_weight = 0.01;
uniform float damping_coefficient = 0.99999;

// Time varying uniforms
uniform float timestep;

void main(void)
{
    vec3 acceleration = vec3(0.0f, -0.1f, 0.0f);
    vec3 center = vec3(0.0);
    vec3 new_velocity;
    int i;

    // Update position based on prior velocity and timestep
    position_out = particle_position + particle_velocity * timestep;
    // Update velocity based on calculated acceleration
    acceleration = normalize(acceleration) * min(length(acceleration), 10.0);
    new_velocity = particle_velocity * damping_coefficient + acceleration * timestep;
    // Hard clamp speed (mag(velocity) to 10 to prevent insanity
    if (length(new_velocity) > 10.0)
        new_velocity = normalize(new_velocity) * 10.0;
    velocity_out = new_velocity;
    // Write position (not strictly necessary as we're capturing user defined
    // outputs using transform feedback)
    gl_Position = vec4(particle_position * 0.1, 1.0);
}
